// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

// import BulletScript from './子弹'

@ccclass
export default class Game extends cc.Component {

    @property(cc.SpriteFrame)
    bulletIcon: cc.SpriteFrame = null;

    interverNum: number = 0.5;

    @property(cc.Prefab)
    bulletPrefab: cc.Prefab = null;

    static bulletPool: cc.NodePool = new cc.NodePool();

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        this.initPool(50);

        this.schedule(this.onTimer, this.interverNum)
        // this.onTimer()
    }

    start () {

    }

    // update (dt) {}

    onTimer(){
        // 动态创建节点的方式，添加子弹
        // const bulletNode: cc.Node = new cc.Node();
        // const sprite: cc.Sprite = bulletNode.addComponent(cc.Sprite)
        // sprite.spriteFrame = this.bulletIcon;
        // bulletNode.addComponent(BulletScript)
        // bulletNode.setScale(0.02)

        // 动态创建预制体的方式，添加子弹
        // const bulletNode: cc.Node = cc.instantiate(this.bulletPrefab);

        // 从对象池中获取node，添加子弹
        let bulletNode: cc.Node = Game.bulletPool.get();
        if(bulletNode === null){
            bulletNode = cc.instantiate(this.bulletPrefab); // 防止对象池中找不到节点
        }

        bulletNode.parent = this.node;
        bulletNode.y = 50;
    }

    // 初始化对象池
    initPool(len){
        for(let i = 0; i < len; i++){
            let node = cc.instantiate(this.bulletPrefab);
            Game.bulletPool.put(node)
        }
    }
}
